package homework;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;

/**
 * @classname:StringPool
 * @description:
 * @author: Mr.Z
 * @date: 2020/12/19 22:29
 * @version: 0.1
 * @since:1.8
 */
public class StringPool {
    private List<String> pool = new CopyOnWriteArrayList<>();

    public void createStr(){
        String str = "qwertyuiopasdfghjklzxcvbnm";
        ThreadLocalRandom localRandom =ThreadLocalRandom.current();
        int i = localRandom.nextInt(0, str.length());
        char charAt = str.charAt(i);
        String s = String.valueOf(charAt);
        pool.add(s);
        System.out.println(Thread.currentThread().getName()+"生产了字符"+s+"当前字符数量"+pool.size());
        try {
            TimeUnit.MILLISECONDS.sleep(150);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    public void deleteStr(){
        if(pool.size()>0){
            pool.remove(0);
            System.out.println(Thread.currentThread().getName()+"处理了一个字符,剩余字符"+pool.size());
            try {
                TimeUnit.MILLISECONDS.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }


    }
}
